import 'package:learning/networks/common/base_response.dart';

class UserBrief {
  int id;

  String type;

  String name;

  String? gender;

  String? avatar;

  DateTime createdAt;

  List<int> purchasingCourses;

  List<int> purchasedCourses;

  UserBrief({
    required this.id,
    required this.type,
    required this.name,
    this.gender,
    this.avatar,
    required this.createdAt,
    this.purchasingCourses = const [],
    this.purchasedCourses = const [],
  });

  factory UserBrief.fromJson(Map<String, dynamic> json) {
    return UserBrief(
      id: (json['id'] as num).toInt(),
      type: json['type'] as String,
      name: json['name'] as String,
      gender: json['gender'] as String?,
      avatar: json['avatar'] as String?,
      createdAt: DateTime.parse(json['created_at'] as String),
      purchasingCourses: (json['purchasing_courses'] as List<dynamic>?)
              ?.map((e) => (e as num).toInt())
              .toList() ??
          const [],
      purchasedCourses: (json['purchased_courses'] as List<dynamic>?)
              ?.map((e) => (e as num).toInt())
              .toList() ??
          const [],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'type': type,
      'name': name,
      'gender': gender,
      'avatar': avatar,
      'createdAt': createdAt.toIso8601String(),
      'purchasingCourses': purchasingCourses,
      'purchasedCourses': purchasedCourses,
    };
  }
}

class UserBriefResponse extends BaseResponse {
  String? token;

  UserBrief? brief;

  UserBriefResponse({
    required super.code,
    super.message,
    this.token,
    required this.brief,
  });

  factory UserBriefResponse.fromJson(Map<String, dynamic> json) {
    return UserBriefResponse(
      code: json['code'] as int,
      message: json['message'] as String?,
      token: json['token'] as String?,
      brief: json['brief'] == null
          ? null
          : UserBrief.fromJson(json['brief'] as Map<String, dynamic>),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'code': code,
      'message': message,
      'token': token,
      'brief': brief?.toJson(),
    };
  }
}
